﻿using DataProcess.DataAccess;
using DataProcess.Enums;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace DataProcess.Views
{
    public partial class DataListFrom<T> : DevExpress.XtraBars.Ribbon.RibbonForm where T : class, new()
    {
        protected virtual DbContext<T> DBContext { get; set; }

        public DataListFrom()
        {
            DBContextInit();
            InitializeComponent();
        }

        private void DataListFrom_Load(object sender, EventArgs e)
        {
            GetRecords();
        }

        protected virtual void DBContextInit()
        {
            DBContext = new DbContext<T>();
        }

        private void GetRecords()
        {
            var result = DBContext.CurrentDb.GetList();
            gridControl.DataSource = new BindingList<T>(result);
            gridView.BestFitColumns();
        }

        private void PrintAndExt_BBI_ItemClick(object sender, ItemClickEventArgs e)
        {
            gridControl.ShowRibbonPrintPreview();
        }

        private void BBI_New_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowDetailsForm(new T(), DataEditType.Add, -1);
        }

        private void BBI_Edit_ItemClick(object sender, ItemClickEventArgs e)
        {
            int rowHandle = gridView.FocusedRowHandle;
            if (rowHandle < 0) return;
            if (gridView.GetRow(rowHandle) is T item)
            {
                ShowDetailsForm(item, DataEditType.Modify, rowHandle);
            }
        }

        private void Del_BBI_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (XtraMessageBox.Show(this, "您确定要删除此条记录么?\r\n点击确定将会从数据库中将该条记录删除切不可恢复!", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK)
            {
                int rowHandle = gridView.FocusedRowHandle;
                if (rowHandle < 0) return;
                if (gridView.GetRow(rowHandle) is T item)
                {
                   
                    if (DBContext.CurrentDb.Delete(item))
                    {
                        gridView.DeleteRow(rowHandle);
                    }
                }
            }
        }

        private void ShowDetailsForm(T t, DataEditType det, int rowhandle)
        {
            var details = new DataDetailsForm<T>(t, det, rowhandle);
            details.UpdateMainRow += UpdateRowData;
            if (details.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { }
            details?.Dispose();
        }

        private void UpdateRowData(int rowHandle, T newItem, DataEditType fet)
        {
            if (newItem == null) return;
            if (gridControl.DataSource is BindingList<T> source)
            {
                switch (fet)
                {
                    case DataEditType.Add:
                        source.Add(newItem);
                        break;
                    case DataEditType.Modify:
                        int sourceIdx = gridView.GetDataSourceRowIndex(rowHandle);
                        source[sourceIdx] = newItem;
                        break;
                }
            }
        }

        private void Ref_BBI_ItemClick(object sender, ItemClickEventArgs e)
        {
            GetRecords();
        }
    }
}